﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;

namespace CafeOnline.MvcFramework.Web {
    public class NewtonJsonResult : JsonResult {
        private static JsonSerializer s_serializer;

        static NewtonJsonResult() {
            s_serializer = new JsonSerializer();
            //s_serializer.Converters.Add(new LocalizedStringEnumConverter());
            //s_serializer.Converters.Add(new DateTimeJsonConverter());
        }


        public override void ExecuteResult(ControllerContext context) {
            if (context == null) {
                throw new ArgumentNullException("context");
            }
            if (this.JsonRequestBehavior == JsonRequestBehavior.DenyGet && string.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase)) {
                throw new InvalidOperationException("JsonRequest_GetNotAllowed");
            }
            HttpResponseBase response = context.HttpContext.Response;
            if (!string.IsNullOrEmpty(this.ContentType)) {
                response.ContentType = this.ContentType;
            } else {
                response.ContentType = "application/json";
            }
            if (this.ContentEncoding != null) {
                response.ContentEncoding = this.ContentEncoding;
            }
            if (this.Data != null) {
                s_serializer.Serialize(response.Output, this.Data);
            }
        }
    }
}
